﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class InventoryTypeCtrl
    {
        InventoryTypeDAO m_TypeDAO = new InventoryTypeDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "TypeName";
            cmb.ValueMember = "TypeId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "TypeName";
            cmb.ValueMember = "TypeId";
            cmb.DataPropertyName = "TypeId";
            cmb.HeaderText = "Loại thống kê";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "TypeName";
            cmb.ValueMember = "TypeId";
            cmb.DataPropertyName = "TypeId";
            cmb.HeaderText = "Loại thống kê";
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtTypeId,
                         TextBoxX txtTypeName,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtTypeId.DataBindings.Clear();
            txtTypeId.DataBindings.Add("Text", bs, "TypeId");

            txtTypeName.DataBindings.Clear();
            txtTypeName.DataBindings.Add("Text", bs, "TypeName");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtTypeId,
                         TextBoxX txtTypeName,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtTypeId.DataBindings.Clear();
            txtTypeId.DataBindings.Add("Text", bs, "TypeId");

            txtTypeName.DataBindings.Clear();
            txtTypeName.DataBindings.Add("Text", bs, "TypeName");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_TypeDAO.GetAll();
        }

        public DataTable GetById(String typeId)
        {
            return m_TypeDAO.GetById(typeId);
        }

        public DataTable GetByLikeId(String typeId)
        {
            return m_TypeDAO.GetByLikeId(typeId);
        }

        public DataTable GetByName(String typeName)
        {
            return m_TypeDAO.GetByName(typeName);
        }

        public int Add(InventoryTypeInfo info)
        {
            return m_TypeDAO.Add(info);
        }

        public int Update(InventoryTypeInfo info)
        {
            return m_TypeDAO.Update(info);
        }

        public int Delete(String typeId)
        {
            return m_TypeDAO.Delete(typeId);
        }

        public DataRow AddNewRow()
        {
            return m_TypeDAO.AddNewRow();
        }

        public void AddNewInventory(DataRow m_Row)
        {
            m_TypeDAO.AddNewInventory(m_Row);
        }

        public bool SaveData()
        {
            return m_TypeDAO.SaveData();
        }
    }
}
